package com.cn.jdia.tas.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.jdia.tas.model.T_tasaccess;
import com.cn.jdia.tas.model.U_department;
import com.cn.jdia.tas.service.IDepartmentService;
import com.cn.jdia.tas.service.ITasAccessService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping("/control")
public class AuthorityController {

	@Resource
	private ITasAccessService tasaccessService = null;

	@Resource
	private IDepartmentService deptService = null;

	@RequestMapping("/deptauthority")
	public String getdeptauthority(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");
		Gson gson = new Gson();
		Map<String, String> pmap = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String pid = pmap.get("pid");
		System.out.println(pid);

		T_tasaccess access = tasaccessService.getDeptAuthority(pid);

		Map<String, String> rmap = new HashMap<>();
		rmap.put("authority", access.getBak1());
		
		U_department parentDept = deptService.getParentDept(access.getDept1());
		if(parentDept==null){
			rmap.put("deptid", access.getDept1());
			rmap.put("deptname", deptService.getDeptDetailInfo(access.getDept1()).getDeptname());
			rmap.put("isparent","false");
		}else{
			rmap.put("deptid", parentDept.getDeptid());
			rmap.put("deptname", parentDept.getDeptname());
			rmap.put("isparent","true");
		}
		
		System.out.println(gson.toJson(rmap));

		out.write(gson.toJson(rmap));
		out.flush();
		out.close();

		return null;
	}

}
